#!/usr/local/bin/perl

use strict;
use Cwd;
use Data::Dumper;
use lib ("/home/sgetest/lib");
use TIGR::HTCRequest;
use TIGR::HTCUtils;
use Log::Log4perl;

my $log_conf = q~
log4perl.rootLogger=DEBUG, client
log4perl.appender.client=Log::Log4perl::Appender::File
log4perl.appender.client.mode=append
log4perl.appender.client.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.client.layout.ConversionPattern=%H [%d{MM/dd HH:mm}] %p %M - %m%n
~;


# setup logging
#
my $logfile = "/usr/local/scratch/$ENV{USER}/arr-singlejob-array/submit.log";
$log_conf .= "log4perl.appender.client.filename=$logfile\n";
Log::Log4perl::init(\$log_conf);
my $logger = Log::Log4perl->get_logger(__PACKAGE__);

# Before starting the test, count the perl processes
my $proc_cnt = TIGR::HTCUtils->get_perl_process_count();
#$logger->info("Active perl process count is : $proc_cnt");
print "Active perl process count is : $proc_cnt \n";
if ($proc_cnt > 20) {
        #print "Active perl process count exceeded max limit of 20. Skipping the launch of new test..\n";
        $logger->info("Active perl process count exceeded max limit of 20. Skipping the launch of new test..");
	TIGR::HTCUtils->log_test_skip("test_htc_singlejob_array");
        exit(1);
}

TIGR::HTCUtils->log_test_start("test_htc_singlejob_array");

$logger->info("Starting array array job test.");
my $request = TIGR::HTCRequest->new(group => "test-htc-array",
                                    initialdir => cwd(),
                                    opsys=>"Linux,Solaris");
    
$request->set_command("/usr/local/scratch/$ENV{USER}/arr-singlejob-array/test_array.pl");

#my @asmbls = (1..100);
my @asmbls = (1);
    
$request->add_param({key => "--asmbl_id", value => \@asmbls, type => "ARRAY"});

$request->set_output("/usr/local/scratch/$ENV{USER}/arr-singlejob-array/htc_array.\$(Index).out");
$request->set_error("/usr/local/scratch/$ENV{USER}/arr-singlejob-array/htc_array.\$(Index).err");

$request->set_getenv(1);

#$request->length('long');


my $id = $request->submit();

my $xx = $request->to_xml();
print $xx;

print "Request id was $id \n";

$request->wait_for_request();

my $message = $request->get_message();
$logger->info("Finished array array job test. Message: $message");
if($request->get_state() eq "FAILURE") {
    	print " Job failed: $message \n";
	exit(1);
} else {
    	print " Request finished with state " . $request->get_state() . " and $message \n";
	# If the test finished then get the task information
	my $tasks = $request->get_tasks();
	print Dumper($tasks);
}

TIGR::HTCUtils->log_test_finish("test_htc_singlejob_array");
print "Finished!\n\n";
exit(0);
